home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-04 / gs24src.zip / ZVMEM.C < prev   
C/C++ Source or Header  |  1992-02-15  |  6KB  |  187 lines

  1. /* Copyright (C) 1989, 1991 Aladdin Enterprises.  All rights reserved.
  2.    Distributed by Free Software Foundation, Inc.
  3.  
  4. This file is part of Ghostscript.
  5.  
  6. Ghostscript is distributed in the hope that it will be useful, but
  7. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  8. to anyone for the consequences of using it or for whether it serves any
  9. particular purpose or works at all, unless he says so in writing.  Refer
  10. to the Ghostscript General Public License for full details.
  11.  
  12. Everyone is granted permission to copy, modify and redistribute
  13. Ghostscript, but only under the conditions described in the Ghostscript
  14. General Public License.  A copy of this license is supposed to have been
  15. given to you along with Ghostscript so you can know your rights and
  16. responsibilities.  It should be in a file named COPYING.  Among other
  17. things, the copyright notice and this notice must be preserved on all
  18. copies.  */
  19.  
  20. /* zvmem.c */
  21. /* "Virtual memory" operators for Ghostscript */
  22. #include "ghost.h"
  23. #include "errors.h"
  24. #include "oper.h"
  25. #include "alloc.h"
  26. #include "estack.h"            /* for checking in restore */
  27. #include "dict.h"            /* ditto */
  28. #include "save.h"
  29. #include "state.h"
  30. #include "store.h"
  31. #include "gsmatrix.h"            /* for gsstate.h */
  32. #include "gsstate.h"
  33.  
  34. /* Imported operators */
  35. extern int zgsave(P1(os_ptr));
  36. extern int zgrestore(P1(os_ptr));
  37.  
  38. /* Import the routine for setting the attributes of the identity matrix. */
  39. extern void init_identity_matrix(P0());
  40.  
  41. /* 'Save' structure */
  42. typedef struct vm_save_s vm_save;
  43. struct vm_save_s {
  44.     alloc_save *asave;        /* allocator save */
  45.     int_gstate isave;        /* old interpreter state */
  46.     gs_state *gsave;        /* old graphics state */
  47. };
  48.  
  49. /* save */
  50. int
  51. zsave(register os_ptr op)
  52. {    vm_save *vmsave = (vm_save *)alloc(1, sizeof(vm_save), "zsave");
  53.     alloc_save *asave;
  54.     int code;
  55.     gs_state *prev, *prev2;
  56.     if ( vmsave == 0 ) return e_VMerror;
  57.     asave = alloc_save_state();
  58.     if ( asave == 0 )
  59.        {    alloc_free((char *)vmsave, 1, sizeof(vm_save), "zsave");
  60.         return e_VMerror;
  61.        }
  62.     vmsave->asave = asave;
  63.     /* Save the old interpreter state, */
  64.     /* and cut the chains so we can't grestore past here. */
  65.     vmsave->isave = istate;
  66.     code = zgsave(op);
  67.     if ( code < 0 ) return code;
  68.     /* Swap the contents of the old and new states, */
  69.     /* so the new state points to the newly allocated components. */
  70.     prev = gs_state_swap_saved(igs, (gs_state *)0);
  71.     prev2 = gs_state_swap_saved(prev, (gs_state *)0);
  72.     gs_state_swap_saved(igs, prev2);
  73.     gs_state_swap(igs, prev);
  74.     vmsave->gsave = igs;
  75.     igs = prev;
  76.     istate.saved = 0;
  77.     push(1);
  78.     make_tv(op, t_save, psave, vmsave);
  79.     init_identity_matrix();        /* update l_new attribute */
  80.     return zgsave(op);
  81. }
  82.  
  83. /* restore */
  84. private int restore_check_stack(P3(ref *, ref *, alloc_save *));
  85. private void restore_fix_stack(P3(ref *, ref *, alloc_save *));
  86. int
  87. zrestore(register os_ptr op)
  88. {    vm_save *vmsave;
  89.     alloc_save *asave;
  90.     check_type(*op, t_save);
  91.     vmsave = op->value.psave;
  92.     if ( vmsave == 0 )        /* invalidated save */
  93.         return e_invalidrestore;
  94.     asave = vmsave->asave;
  95.     /* Check the contents of the stacks. */
  96.        {    int code;
  97.         if ( (code = restore_check_stack(osbot, op, asave)) < 0 ||
  98.              (code = restore_check_stack(esbot, esp + 1, asave)) < 0 ||
  99.              (code = restore_check_stack(dstack, dsp + 1, asave)) < 0
  100.            )
  101.             return code;
  102.        }
  103.     if ( alloc_restore_state_check(asave) < 0 )
  104.         return e_invalidrestore;
  105.     /* Invalidate any other copies of this save object on the stacks. */
  106.     restore_fix_stack(osbot, op, asave);
  107.     restore_fix_stack(esbot, esp + 1, asave);
  108.     /* Now it's safe to restore the state of memory. */
  109.     alloc_restore_state(asave);
  110.     /* Restore the interpreter and graphics state. */
  111.     istate = vmsave->isave;
  112.     igs = vmsave->gsave;
  113.     alloc_free((char *)vmsave, 1, sizeof(vm_save), "zrestore");
  114.     pop(1);
  115.     init_identity_matrix();        /* update l_new attribute */
  116.     return 0;
  117. }
  118. /* Check a stack to make sure all its elements are older than a save. */
  119. private int
  120. restore_check_stack(ref *bot, ref *top, alloc_save *asave)
  121. {    ref *stkp;
  122.     for ( stkp = bot; stkp < top; stkp++ )
  123.        {    char *ptr;
  124.         switch ( r_type(stkp) )
  125.            {
  126.         case t_array: ptr = (char *)stkp->value.refs; break;
  127.         case t_condition: ptr = (char *)stkp->value.pcond; break;
  128.         case t_dictionary: ptr = (char *)stkp->value.pdict; break;
  129.         case t_fontID: ptr = (char *)stkp->value.pfont; break;
  130.         case t_gstate: ptr = (char *)stkp->value.pgstate; break;
  131.         /* case t_file: ****** WHAT? ****** */
  132.         case t_lock: ptr = (char *)stkp->value.plock; break;
  133.         case t_name:
  134.             /* Names are special because of how they are allocated. */
  135.             if ( alloc_name_is_since_save(stkp, asave) )
  136.                 return e_invalidrestore;
  137.             continue;
  138.         case t_save: ptr = (char *)stkp->value.psave; break;
  139.         case t_string: ptr = (char *)stkp->value.bytes; break;
  140.         case t_mixedarray: case t_shortarray:
  141.             ptr = (char *)stkp->value.packed; break;
  142.         case t_color: ptr = (char *)stkp->value.pcolor; break;
  143.         case t_device: ptr = (char *)stkp->value.pdevice; break;
  144.         default: continue;
  145.            }
  146.         if ( alloc_is_since_save(ptr, asave) )
  147.             return e_invalidrestore;
  148.        }
  149.  
  150.     return 0;            /* OK */
  151. }
  152. /* Fix up the contents of a stack by invalidating */
  153. /* any save objects newer than the save being restored. */
  154. private void
  155. restore_fix_stack(ref *bot, ref *top, alloc_save *asave)
  156. {    ref *stkp;
  157.     for ( stkp = bot; stkp < top; stkp++ )
  158.        {    if ( r_type(stkp) == t_save &&
  159.              stkp->value.psave != 0 &&
  160.              (stkp->value.psave->asave == asave ||
  161.               alloc_is_since_save((char *)stkp->value.psave, asave))
  162.            )
  163.             stkp->value.psave = 0;
  164.        }
  165. }
  166.  
  167. /* vmstatus */
  168. int
  169. zvmstatus(register os_ptr op)
  170. {    long used, total;
  171.     alloc_status(&used, &total);
  172.     push(3);
  173.     make_int(op - 2, alloc_save_level());
  174.     make_int(op - 1, used);
  175.     make_int(op, total);
  176.     return 0;
  177. }
  178.  
  179. /* ------ Initialization procedure ------ */
  180.  
  181. op_def zvmem_op_defs[] = {
  182.     {"1restore", zrestore},
  183.     {"0save", zsave},
  184.     {"0vmstatus", zvmstatus},
  185.     op_def_end(0)
  186. };
  187.